#pragma once
#include <vector>
#include <cstring>
#include <iostream>
#include <cstdio>
#define SEP " "
#define SEP_LEN strlen(SEP)
#include"myUtil.hpp"
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include"myLog.hpp"
#define HEADER "\r\n"
#define HEADER_LEN strlen(HEADER)
//#define MYSELF 1
#include <jsoncpp/json/json.h>

//"7"\r\n"10 + 20"\r\n

void RemoveHeader(string& str)
{
    //logMessge(DEBUG,"去报头之前：%s",str.c_str());
    
    auto pos=str.find(HEADER);
    auto LenStr=str.substr(0,pos);
    auto Len=toInt(LenStr);
    str=str.substr(pos+HEADER_LEN,pos+HEADER_LEN+Len);
    //logMessge(DEBUG,"去报头之后：%s",str.c_str());

}
void AddHeader(string& str)
{
    //logMessge(DEBUG,"加报头之前：%s",str.c_str());
    auto s=str.size();
    auto strLen=to_string(s);
    auto strHeader=strLen+HEADER;
    str=strHeader+str+HEADER;
    //logMessge(DEBUG,"加报头之后：%s",str.c_str());


}

int ReadPackage(int sock,string& inbuffer,string* bufStr)
{
    char buf[1024];
    auto s=recv(sock,buf,sizeof(buf)-1,0);
    if(s<=0)
    {
        logMessge(FATAL,"没收到消息！");
        return -1;//没收到消息

    }
    buf[s]=0;
    inbuffer+=buf;
    auto pos=inbuffer.find(HEADER);
    string LenStr=inbuffer.substr(0,pos);
    auto TarPackageLen=toInt(LenStr)+LenStr.size()+2*HEADER_LEN;
    if(inbuffer.size()<TarPackageLen)
    {
        logMessge(WARING,"没读取完整！");

        return 0;//没读完整
    }
    *bufStr=inbuffer.substr(0,TarPackageLen);
    inbuffer.erase(0,TarPackageLen);
    return 1;//读完整
}


class Response
{
public:
    Response(int result,int code):_result(result),_code(code)
    {
    }
    Response()
    {
    }
    ~Response()
    {
    }
    bool Serialize(string&outStr)
    {
#ifdef MYSELF
        string _result_string=to_string(_result);
        string _code_string=to_string(_code);
        outStr=_result_string+SEP+_code_string;
        //logMessge(DEBUG,"序列化之后：%s",outStr->c_str());
#else 
        Json::Value root;
        root["result"]=_result;
        root["code"]=_code;
        Json::StyledWriter write;
        outStr=write.write(root); 
#endif
        return true;

    }
    bool DeSerialize(const string &str)
    {
#ifdef MYSELF
        vector<string> vs;
        //logMessge(DEBUG,"反序列化之前：%s",str.c_str());

        stringSeparate(str,&vs);
        if(vs.size()!=2)
        {
            cout<<"vs"<<endl;
            return false;
        }
        _result=toInt(vs[0]);
        _code=toInt(vs[1]);
        //logMessge(DEBUG,"反序列化之后：%d %d",_result,_code);
#else
    Json::Value root;
    Json::Reader reader;
    reader.parse(str,root) ;
    _result=root["result"].asInt();
    _code=root["code"].asInt();

    
#endif
    }

public:
    int _result;
    int _code;
};

class Request
{
public:
    Request()
    {
    }
    ~Request()
    {
    }
    bool Serialize(string* str)
    {
        *str="";
#ifdef MYSELF
        *str=to_string(_x)+SEP+to_string(_op)+SEP+to_string(_y);
#else
    Json::Value root;
    root["x"]=_x;
    root["op"]=_op;
    root["y"]=_y;
    Json::StyledWriter write;
    *str=write.write(root);

#endif
        return true;
    }
    bool DeSerialize(const string &str)
    {
#ifdef MYSELF
        vector<string> vs;
        int i=0;
        stringSeparate(str, &vs);
        if(vs.size()!=3)
        {
            return false;

        }
        
         auto op=toInt(vs[1]);
        if(!(op=='+'||op=='-'||op=='*'||op=='/'||op=='%'))
        {
            logMessge(FATAL,"反序列化错误!");
            return false;
        }
        _x=toInt(vs[0]);
        _op=toInt(vs[1]);
        _y=toInt(vs[2]);
        //cout<<"反序列化之后，test: "<<_x<<_op<<_y<<endl;
#else
    Json::Value root;
    Json::Reader reader;
    reader.parse(str,root) ;
    _x=root["x"].asInt();
    _op=root["op"].asInt();
    _y=root["y"].asInt();
#endif

    }
public:
    int _x;
    int _y;
    char _op;
};
